{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conceptual Overview\n",
    "\n",
    "This paints a picture showing the k-NN concept.\n",
    "\n",
    "An unknown sample, shown as a ◆ is surrounded by two groups of known items, shown as ⚫︎ and ◼︎.\n",
    "\n",
    "In this example, we're using the Euclidean distance between points, computed by `math.hypot`. There are 3 neighbors within a distance of 2.0. Among these, two are ⚫︎, and one is ◼︎. For a k-value of 3, the most common neighbor is ⚫︎.\n",
    "\n",
    "If we switch to k-value of 5, we dicover 3 ◼︎ and 2 ⚫︎, flipping the outcome.\n",
    "\n",
    "This situation is relatively rare and requires an unknown sample to be carefully perched almost midway between two populations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using matplotlib backend: MacOSX\n"
     ]
    }
   ],
   "source": [
    "%matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-34-29c929654b6a>:21: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.\n",
      "  plt.axes().add_patch(circle3)\n",
      "<ipython-input-34-29c929654b6a>:26: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.\n",
      "  plt.axes().add_patch(circle5)\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.path\n",
    "from matplotlib.patches import Circle\n",
    "\n",
    "sample = (5, 5)\n",
    "circles = [(2, 8), (4, 6), (5, 7)]\n",
    "squares = [(4, 2), (6, 2), (6, 4)]\n",
    "\n",
    "plt.figure(\"Concept\", (5, 5))\n",
    "plt.set_cmap('gray')\n",
    "\n",
    "# plt.axis((0, 10, 0, 10), option='equal')\n",
    "plt.axis('equal')\n",
    "plt.scatter(*sample, marker=\"D\", label=\"??\", color='0.0')\n",
    "plt.scatter([x for x, y in circles], [y for x, y in circles], marker=\"o\", color='.20')\n",
    "plt.scatter([x for x, y in squares], [y for x, y in squares], marker=\"s\", color='.33')\n",
    "\n",
    "# k = 3 nearest neighbors\n",
    "circle3 = Circle((5, 5), 2, facecolor='none',\n",
    "                edgecolor='black', linestyle='--', alpha=0.8)\n",
    "plt.axes().add_patch(circle3)\n",
    "\n",
    "# k = 5 nearest neighbors\n",
    "circle5 = Circle((5, 5), 3.2, facecolor='none',\n",
    "                edgecolor='black', linestyle=':', alpha=1.0)\n",
    "plt.axes().add_patch(circle5)\n",
    "\n",
    "\n",
    "plt.grid(True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1.4142135623730951,\n",
       " 1.4142135623730951,\n",
       " 2.0,\n",
       " 3.1622776601683795,\n",
       " 3.1622776601683795,\n",
       " 4.242640687119286]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "sorted(math.hypot((sample[0]-t[0]), (sample[1]-t[1])) for t in circles+squares)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
